package tbalert.web.action;

import javax.servlet.http.HttpServletRequest;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;

import tbalert.page.Pagination;
import tbalert.page.SimplePage;
import tbalert.web.SysConstants;
import tbalert.web.WebErrors;
import tbalert.web.entity.Role;
import tbalert.web.manager.RoleMng;

@Controller
@RequestMapping(value = "/admin/role")
public class RoleAct {
	private static final Logger log = LoggerFactory.getLogger(DwdmAct.class);

	@RequestMapping(value = "/list.do")
	public String list(ModelMap model, HttpServletRequest request, Role role, Integer pageSize,
			Integer pageNo) {
		if (pageSize == null) {
			pageSize = SimplePage.DEF_COUNT;
		}
		if (pageNo == null) {
			pageNo = 1;
		}
		Pagination p = roleMng.findRole(role, pageNo, pageSize);
		model.put("pagination", p);
		model.put("role", role);
		return "/admin/role/list";
	}

	@RequestMapping(value = "/add.do")
	public String add(ModelMap model, HttpServletRequest request) {
		return "/admin/role/add";
	}

	@RequestMapping(value = "/edit.do")
	public String edit(ModelMap model, HttpServletRequest request, Integer id) {
		WebErrors errors = validateEdit(id, request);
		if (errors.hasErrors()) {
			return errors.showErrorPage(model);
		}
		model.put("role", roleMng.findById(id));
		return "/admin/role/edit";
	}

	@RequestMapping(value = "/save.do")
	public String save(ModelMap model, HttpServletRequest request, Role role) {
		WebErrors errors = validateSave(role, request);
		if (errors.hasErrors()) {
			return errors.showErrorPage(model);
		}
		roleMng.save(role);
		return "redirect:list.do";
	}

	@RequestMapping(value = "/update.do")
	public String update(ModelMap model, HttpServletRequest request, Role role) {
		WebErrors errors = validateUpdate(role.getId(), request);
		if (errors.hasErrors()) {
			return errors.showErrorPage(model);
		}
		roleMng.update(role);
		log.info("update Role id={}.", role.getId());
		model.put(SysConstants.MESSAGE, "global.success");
		return list(model, request, new Role(), SimplePage.DEF_COUNT, 1);
	}

	@RequestMapping(value = "/delete.do")
	public String delete(ModelMap model, HttpServletRequest request, Integer id) {
		Role role = roleMng.deleteById(id);
		if (role == null) {
			model.put(SysConstants.MESSAGE, "尚有单位属于次角色，删除失败！");
		} else {
			log.info("delete Role id={}.", id);
			model.put(SysConstants.MESSAGE, "global.success");
		}
		return list(model, request, new Role(), SimplePage.DEF_COUNT, 1);
	}

	@Autowired(required = true)
	private RoleMng roleMng;

	private WebErrors validateSave(Role bean, HttpServletRequest request) {
		WebErrors errors = new WebErrors(request);
		return errors;
	}

	private WebErrors validateEdit(Integer id, HttpServletRequest request) {
		WebErrors errors = new WebErrors(request);
		if (vldExist(id, errors)) {
			return errors;
		}
		return errors;
	}

	private WebErrors validateUpdate(Integer id, HttpServletRequest request) {
		WebErrors errors = new WebErrors(request);
		if (vldExist(id, errors)) {
			return errors;
		}
		return errors;
	}

	private boolean vldExist(Integer id, WebErrors errors) {
		if (errors.ifNull(id, "id")) {
			return true;
		}
		Role entity = roleMng.findById(id);
		if (errors.ifNotExist(entity, Role.class, id)) {
			return true;
		}
		return false;
	}
}
